﻿var map;
var geocoder;
// set the default loaction
var latlong = new GLatLng(54.49556752187406, -4.21875);
// set the default zoom level
var level = 5;

function initialize() {
    map = new GMap2(document.getElementById("map_canvas_events"));

    map.enableScrollWheelZoom();
    //map.enableContinuousZoom();  
    // map.addControl(new GMapTypeControl());
    //map.addControl(new GLargeMapControl());

    map.addControl(new GSmallMapControl());

    map.setCenter(latlong, level);
    geocoder = new GClientGeocoder();

}

// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
    map.clearOverlays();
    if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
    } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
        marker = new GMarker(point, { draggable: true });
        map.addOverlay(marker);
        marker.openInfoWindowHtml(place.address + '<br>');


        GEvent.addListener(marker, "dragstart", function() {
            map.closeInfoWindow();
        });


        GEvent.addListener(marker, "dragend", function() {
            point = marker.getPoint();
            geocoder.getLocations(point, function(addresses) {
            if (addresses.Status.code != 200) {
                document.getElementById("address-textbox").value = "unknown location"
                    alert("failed to find an address for " + point.toUrlValue());
                }
                else {
                    address = addresses.Placemark[0];
                    var myHtml = '<b>Address:</b>&nbsp;' + address.address + '<br/>' +
		                '<b>Updated Coordinates:</b>&nbsp;' + point.toUrlValue();
                    document.getElementById("address-textbox").value = address.address;
                    marker.openInfoWindow(myHtml);
                }
            });
        });
    }
}

// showLocation() is called when you click on the Search button
// in the form.  It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation() {
    var address = document.getElementById("address-textbox").value;
    geocoder.getLocations(address, addAddressToMap);
}

// findLocation() is used to enter the sample addresses into the form.
function findLocation(address) {
    document.getElementById("address-textbox").value = address;
    showLocation();
}




// set the event 
function addEvent(a, b, c) {

    if (window.addEventListener) {
        window.addEventListener(a, b, typeof (c) != 'undefined' ? c : 0);
    }
    else if (window.attachEvent) {
        window.attachEvent('on' + a, b);
    }
    else {
        window['on' + a] = b;
    }
}
